const Promise = require('bluebird');

module.exports = function (client, env) {

    return {
        /**
         * 删除数据库
         */
        drop: function () {
            return new Promise(function (resolve, reject) {
                client.indices.exists({index: 'goodses_' + env}, function (error, response) {
                    if (error) {
                        return reject(error);
                    }
                    if (response) {
                        client.indices.delete({index: 'goodses_' + env}, function (error, response) {
                            if (error) {
                                return reject(error);
                            }
                            return resolve({success: true, deleted: true, index: 'goodses_' + env})
                        });
                    } else {
                        return resolve({success: true, deleted: false, index: 'goodses_' + env});
                    }
                });
            });
        },

        /**
         * 索引记录
         */
        index: function (goods) {
            return new Promise(function (resolve, reject) {
                let body = {
                    type: goods.type,
                    rootCatId: goods.rootCatId,
                    leafCatId: goods.leafCatId,
                    userType: goods.userType,
                    shopTitle: goods.shopTitle,
                    title: goods.title,
                    auctionId: goods.auctionId,
                    sellerId: goods.sellerId,
                    biz30day: goods.biz30day,
                    isNew: goods.isNew,
                    isHot: goods.isHot,
                    isJYJ: goods.isJYJ,
                    isJhs: goods.isJhs,
                    isTqg: goods.isTqg,
                    isPP: goods.isPP,
                    isYFX: goods.isYFX,
                    isJP: goods.isJP,
                    isHT: goods.isHT,
                    is30: goods.is30,
                    is99: goods.is99,
                    is199: goods.is199,
                    feature: goods.feature,
                    order_by: goods.order_by
                };
                
                client.index({
                    index: 'goodses_' + env,
                    type: 'goodses',
                    id: goods.id,
                    body: body
                }, function (error, response) {
                    if (error) {
                        return reject(error);
                    }
                    return resolve({success: true})
                });
            });
        },

        /**
         * 删除记录
         */
        delete: function (id) {
            return new Promise(function (resolve, reject) {
                client.delete({
                    index: 'goodses_' + env,
                    type: 'goodses',
                    id: id
                }, function (error, response) {
                    if (error) {
                        return reject(error);
                    }
                    return resolve({success: true});
                });
            });
        },

        /**
         * 搜索记录
         */
        search: function (query) {
            if (!query.keyword) {
                return Promise.resolve({count: 0, rows: []});
            }
            query.keyword = query.keyword.replace(/[\|, \/, :, \.,\\, \*, \?, \!, \#, 【, 】,\[, \]]/g, '');

            return new Promise(function (resolve, reject) {
                client.search({
                    index: 'goodses_' + env,
                    type: 'goodses',
                    q: 'title:' + query.keyword,
                    from: (query.offset || 0),
                    size: (query.limit || 20)
                }, function (error, response) {
                    if (error) {
                        return reject(error);
                    }
                    let {total, hits} = response.hits, rows = [];
                    for (let i in hits) {
                        let source = hits[i]._source;
                        source.id = hits[i]._id;
                        rows.push(source);
                    }
                    return resolve({rows: rows, count: total});
                });
            }).catch(function (err) {
                if (err) {
                    console.error('\n', err.stack || err.message)
                }
                return {rows: [], count: 0}
            });
        },
    }
}